import React, { useEffect } from 'react'
import configRouter from './router/router'
import { useRoutes, useLocation } from 'react-router-dom'
import { useDispatch, useSelector } from'react-redux'
import type { AppDispatch, RootState } from'./store/store'
import { getUserInfoAction } from './store/models/userInfo'
import { Spin } from 'antd'

const App:React.FC = () => {
  const dispatch:AppDispatch = useDispatch()
  const location = useLocation()
  const loading = useSelector((state:RootState) => state.user.userLoading)
  const routes = useRoutes(configRouter)
  // 刚进页面调用用户信息接口
  useEffect(() => {
    if(location.pathname !== '/user/login') {
      dispatch(getUserInfoAction())
    }
    
  },[])

  return (
    <Spin spinning={loading}>
      {routes}
    </Spin>
  )
}

export default App